package stark.app.mine;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.stark.more.about.DefAboutActivity;

import stark.app.MainBaseFragment;
import stark.app.frame.TextItemBean;
import stark.app.mine.bean.VersionInfo;
import stark.common.basic.utils.DialogUtil;

/**
 * Author：daizhongbin on 2023/5/8
 * Email：1027154338@qq.com
 * Desc:
 */
public class MineFragment extends MainBaseFragment {

    private static final String UPGRADE_DETECT = "升级检测";
    private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        TextItemBean b = mAdapter.getItem(position);
        if (b.getDestCls() != null) {
            startActivity(new Intent(mContext, b.getDestCls()));
            return;
        }

        if (UPGRADE_DETECT.equals(b.getName())) {
            upgradeCheck();
        }
    }

    @Override
    public void addDefaultData() {
        mAdapter.addData(new TextItemBean("关于我们", DefAboutActivity.class));
        mAdapter.addData(new TextItemBean(UPGRADE_DETECT, null));
    }

    private void upgradeCheck() {
        PermissionUtils.permission(permissions)
                .callback(new PermissionUtils.SimpleCallback() {
                    @Override
                    public void onGranted() {
                        showDialog("检测中...");
                        UpgradeManager.getInstance().check(new UpgradeManager.ICheckCallback() {
                            @Override
                            public void onCheckSuccess(boolean hasNewVersion, @Nullable VersionInfo versionInfo) {
                                dismissDialog();
                                if (hasNewVersion) {
                                    showUpgradeDialog(versionInfo);
                                }
                            }
                        });
                    }

                    @Override
                    public void onDenied() {
                        ToastUtils.showShort("请先授予权限");
                    }
                })
                .request();
    }

    private void showUpgradeDialog(VersionInfo versionInfo) {
        String title = "升级提示";
        String content = "检测到新版本，确认是否升级？";
        String cancel = "取消";
        String confirm = "升级";
        DialogUtil.asConfirm(getContext(), title, content, cancel, confirm, new OnConfirmListener() {
            @Override
            public void onConfirm() {
                showDialog("下载中...0%");
                UpgradeManager.getInstance().downloadApk(versionInfo, new UpgradeManager.IDownloadApkCallback() {
                    @Override
                    public void onProgress(int percent) {
                        showDialog("下载中..." + percent + "%");
                    }

                    @Override
                    public void onFail() {
                        ToastUtils.showShort("下载失败");
                        dismissDialog();
                    }

                    @Override
                    public void onComplete(Uri uri) {
                        dismissDialog();
                        //跳去安装界面
                        startActivity(IntentUtils.getInstallAppIntent(uri));
                    }
                });
            }
        }, null, false);
    }

}
